<?php

namespace App\Http\Controllers\Admin;
use App\Http\Requests\Admin\LinkSite\CreateRequest;
use App\Http\Requests\Admin\LinkSite\UpdateRequest;
use App\Models\Admin\LinkSite;
use Illuminate\Http\Request;
use Style;

class LinkSiteController extends CommonController{
    /**
     * 显示友情站点
     * @return mixed
     */
    public function index(){
        $linkSite = LinkSite::orderBy('updated_at','desc')->paginate(20);
        return Style::view('admin.links.link_site.index',compact('linkSite'));
    }

    /**
     * 添加友情站点
     * @return mixed
     */
    public function create(){
        return Style::view('admin.links.link_site.create');
    }

    /**
     * 添加友情站点提交
     * @param CreateRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(CreateRequest $request){
        $input = $request->only(['link_name','link_sign']);
        $result = LinkSite::create($input);
        if($result){
            flash('添加友情站点管理成功', 'success');
            return redirect('admin/link_site');
        }else{
            return back()->with('errors','友情站点管理添加失败，请稍后重试！');
        }
    }

    /**
     * 显示友情站点
     * @param $id
     */
    public function show($id){ }

    /**
     * 编辑友情站点
     * @param $id
     * @return mixed
     */
    public function edit($id){
        $linkSite = LinkSite::find($id);
        return Style::view('admin.links.link_site.edit',compact('linkSite'));
    }

    /**
     * 更新友情站点
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(UpdateRequest $request, $id){
        $input = $request->only(['link_name','link_sign']);
        $result = LinkSite::where('id',$id)->update($input);
        if($result){
            flash('更新友情站点管理成功', 'success');
            return redirect('admin/link_site');
        }else{
            return back()->with('errors','更新前和更新后数据相同，更新失败！！');
        }
    }

    /**
     * 删除友情站点
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id){
        $result = LinkSite::where('id',$id)->delete();
        if($result){
            return response()->json(['status' => 0,'msg' => '删除友情站点管理成功！']);
        }else{
            return response()->json(['status' => 1,'msg' => '删除友情站点管理失败，请稍后重试！']);
        }
    }
}
